package sgt.supermarket.hibernatedao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import sgt.supermarket.entity.Category;
import sgt.supermarket.entity.Product;

public class HibernateProductDAO extends HibernateGenericDAO<Product, Long> {

    public HibernateProductDAO(Session session) {
        super(session);
    }

    public List<Product> getByKeyword(String keyword) {
        String pattern = '%' + keyword + '%';
        return getSession().createCriteria(Product.class)
                .add(
                    Expression.or(
                        Expression.like("name", pattern),
                        Expression.or(
                            Expression.like("code", pattern),
                            Expression.like("description", pattern)
                        )
                    )
                ).list();
    }

    public List<Product> getByCategory(Category parent) {
        return getSession().createCriteria(Product.class).add(Expression.eq("category.autoId", parent.getAutoId())).list();
    }
}
